﻿package mortal.game.view.mainUI.selectAvatar
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import flash.events.*;
    import frEngine.event.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.rules.*;
    import mortal.game.scene3D.events.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class SelectAvatarController extends Controller
    {
        private var _avatarView:SelectAvatarView;

        public function SelectAvatarController()
        {
            return;
        }// end function

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.Player_Selected, this.onPlayerSelected);
            return;
        }// end function

        override protected function initView() : IView
        {
            if (!this._avatarView)
            {
                this._avatarView = new SelectAvatarView();
            }
            return this._avatarView;
        }// end function

        private function onPlayerSelected(event:DataEvent) : void
        {
            var _loc_3:EntityInfo = null;
            var _loc_4:TBoss = null;
            var _loc_2:* = event.data as IEntity;
            if (_loc_2 && _loc_2.selected)
            {
                _loc_3 = _loc_2.entityInfo;
                if (_loc_2.type == EntityType.NPC)
                {
                    this._avatarView.showBaseAvatar(_loc_3);
                }
                else if (_loc_2.type == EntityType.Pet)
                {
                    this._avatarView.showPetAvatar(_loc_3);
                }
                else if (_loc_2.type == EntityType.Player)
                {
                    if (_loc_3 && _loc_3.entityInfo && EntityUtil.isMercenary(_loc_3.entityInfo.entityId))
                    {
                        this._avatarView.showMercenaryAvatar(_loc_3);
                    }
                    else
                    {
                        this._avatarView.showUseAvatar(_loc_3);
                    }
                }
                else if (_loc_2.type == EntityType.Boss)
                {
                    if (_loc_3 && _loc_3.entityInfo)
                    {
                        _loc_4 = BossConfig.instance.getInfoByCode(_loc_3.entityInfo.code);
                        if (_loc_4 && _loc_4.category == EBossCategory._EBossCategoryBoss)
                        {
                            this._avatarView.showBossAvatar(_loc_3);
                        }
                        else if (_loc_4 && _loc_4.type == EBossType._EBossTypePlayerRobot)
                        {
                            this._avatarView.showRobotAvatar(_loc_3);
                        }
                        else if (_loc_4 && _loc_4.type == EBossType._EBossTypePetRobot)
                        {
                            this._avatarView.showMonsterAvatar(_loc_3);
                        }
                        else
                        {
                            this._avatarView.showMonsterAvatar(_loc_3);
                        }
                    }
                }
                FrEventDispatcher.instance.proxyAddEventListener(_loc_2, PlayerEvent.UPDATEINFO, this.onPlayerInfoUpdate);
            }
            else
            {
                this._avatarView.removeLastAvatar();
                FrEventDispatcher.instance.proxyRemoveEventListener(_loc_2, PlayerEvent.UPDATEINFO, this.onPlayerInfoUpdate);
            }
            return;
        }// end function

        private function onPlayerInfoUpdate(event:Event) : void
        {
            this._avatarView.updateEntity((event.target as IEntity).entityInfo);
            return;
        }// end function

    }
}
